import com.rabbitmq.client.*;

import java.io.IOException;

/**
 * @Author： yu.Tan
 * @Date: 2020/8/19 13:54
 * @Description:
 */
public class ConsumerTest001 {

    private static final String SIMPLE_QUEUE_NAME = "MQ_SIMPLE_QUEUE";

    public static void main(String[] args) {
        //获取MQ连接
        Connection connection = MQConnecitonUtils.getConnection();
        Channel channel=null;

        try {
            //创建消息通道对象
            channel=connection.createChannel();

            //声明队列
            channel.queueDeclare(SIMPLE_QUEUE_NAME,false,false,false,null);

            //创建消费者
            DefaultConsumer consumer=new DefaultConsumer(channel){
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                    String msg = new String(body, "UTF-8");
                    System.out.println("receive message:"+msg);
                }
            };

            channel.basicConsume(SIMPLE_QUEUE_NAME,true,consumer);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }

    }
}
